﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

 
namespace mojoPortal.Web.RS.SBM
{
    public partial class RSEstimateReport : RSBasePage
    {
        int _companyid = 0;
        int _estimateid = 0;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage("Estimate Report");
                RSUserIdentity userIdentity = GetRSUserIdentity();
                _companyid = userIdentity.RSCompanyId;

                if (Request.QueryString["estimateid"] != null)
                    _estimateid = Convert.ToInt32(Request.QueryString["estimateid"].ToString());

                if (_companyid > 0 && _estimateid > 0)
                {
                    LoadEstimateDDL(_companyid);
                    SetReport(_companyid, _estimateid);
                }
                int locationid = RSSBMCommonFunctions.GetLocationIDByEstimateID(_companyid, _estimateid);
                ucLocationHeaderControl.LoadLocationHeader(_companyid, locationid);
            }
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(Session["PreviousURL"].ToString());
        }
        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            _companyid = userIdentity.RSCompanyId;
            _estimateid = Convert.ToInt32(ddlEstimateId.SelectedValue);
            if (_companyid > 0 && _estimateid > 0)
                SetReport(_companyid, _estimateid);
        }
        private void LoadEstimateDDL(int companyid)
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "SELECT EstimateId FROM [rs_tblEstimate] WHERE CompanyID=@companyid ORDER BY EstimateId ASC";
            SqlParameter param = new SqlParameter();
            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);
            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlEstimateId.Items.Clear();
            ddlEstimateId.DataSource = myDataReader;
            ddlEstimateId.DataTextField = "EstimateId";
            ddlEstimateId.DataValueField = "EstimateId";
            ddlEstimateId.DataBind();
            ddlEstimateId.Items.Insert(0, new ListItem("< Please select >", "0"));
            myConnection.Close();
        }

        protected void ObjectDataSource_OnObjectCreated(object sender, ObjectDataSourceEventArgs e)
        {
            if (e.ObjectInstance != null)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings[RSSBMCommonFunctions.GetRSCompanyConnectionString()].ToString();
                e.ObjectInstance.GetType().GetProperty("Connection").SetValue(e.ObjectInstance, conn, null);
            }
        }

        private void SetReport(int companyid, int estimateid)
        {
            ddlEstimateId.SelectedValue = estimateid.ToString();
            EstimateObjectDataSource.SelectParameters["Estimateid"].DefaultValue = estimateid.ToString();
            EstimateObjectDataSource.SelectParameters["companyid"].DefaultValue = companyid.ToString();

            EstimateItemObjectDataSource.SelectParameters["Estimateid"].DefaultValue = estimateid.ToString();
            EstimateItemObjectDataSource.SelectParameters["companyid"].DefaultValue = companyid.ToString();

            CompanyObjectDataSource.SelectParameters["companyid"].DefaultValue = companyid.ToString();

            LocalReport rep = ReportViewer1.LocalReport;
            rep.ReportPath = "RS\\AppData\\SBM\\Reports\\RDLC\\RSEstimateReport.rdlc";
            rep.DataSources.Clear();

            ReportDataSource dsEstimate = new ReportDataSource("vwEstimate", EstimateObjectDataSource);
            ReportDataSource dsEstimateItem = new ReportDataSource("vwEstimateItem", EstimateItemObjectDataSource);
            ReportDataSource dsCompany = new ReportDataSource("tblCompany", CompanyObjectDataSource);

            rep.DataSources.Add(dsEstimate);
            rep.DataSources.Add(dsEstimateItem);
            rep.DataSources.Add(dsCompany);
            rep.Refresh();
        }

    }
}